package edu.szte.ovrt.datahandler.ui.console;

import java.io.*;

public abstract class UIBase {

	protected void newScreen(final String title) {
		System.out.println("################################################################################");
		System.out.printf("%" + (40 + title.length() / 2) + "s", title);
		System.out.println();
		System.out.println("################################################################################");
	}

	protected void showLabel(final String label) {
		System.out.printf("%s: ", label);
	}

	protected void showError(final String message) {
		System.out.printf("ERROR: %s", message);
		System.out.println();
	}

	/**
	 * Hanldes the printing of a line on the screen
	 * 
	 * @param index The index of menu item
	 * @param title The content of a menu item
	 */
	protected void showNumberedLine(final Integer index, final String title) {
		System.out.printf("%7d. %s", index, title);
		System.out.println();
	}

	/**
	 * Shows the label and reads an integer from standard input.
	 * 
	 * @param label The label for the parameter
	 * @return <0 means error, >0 means valid selection (further check may required)
	 */
	protected int readInt(final String label) {
		showLabel(label);
		return readInt();
	}

	/**
	 * Reads an integer selection from the console.
	 * 
	 * @return <0 means error, >0 means valid selection (further check may required)
	 */
	protected int readInt() {
		final BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
		int result = -1;
		try {
			final String s = rd.readLine();
			result = Integer.parseInt(s);
		} catch (final NumberFormatException e) {
			result = -2;
		} catch (final IOException e) {
			result = -3;
		}
		return result;
	}

	/**
	 * Shows the label and reads a string from the standard input
	 * 
	 * @param label The label printed before input
	 * @return The string read from input
	 */
	@Deprecated
	protected String readString(final String label) throws IOException {
		showLabel(label);
		return readString();
	}

	/**
	 * Reads a string value from standard input.
	 * 
	 * @return A string from standard input
	 */
	@Deprecated
	protected String readString() throws IOException {
		final BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
		return rd.readLine();
	}

	protected void showMessage(final String message) {
		System.out.printf("%s", message);
		System.out.println();
	}
}
